<?php
require_once(LIBPATH.'sql.php');

class tableNews extends table
{
    public function __construct()
    {
        parent::table('news', NULL);
        $this->addColumn('id'        , 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY');
        $this->addColumn('user'      , 'varchar(10)  NOT NULL' );
        $this->addColumn('title'     , 'varchar(255) NOT NULL');
        $this->addColumn('subTitle'  , 'varchar(255)');
        $this->addColumn('flags'     , 'varchar(255)');
        $this->addColumn('regDate'   , 'date');
        $this->addColumn('regTime'   , 'time');
        $this->addColumn('news'      , 'text');
        $this->db->query($this->create());
    }


    public function contentSearch($term)
    {
    	$term = '%'.strtoupper($term).'%';
        $this->clear();
    	$this->addWhere('upper(title) like',   $term, 'OR');
    	$this->addWhere('upper(subtitle) like',$term, 'OR');
    	$this->addWhere('upper(news) like',    $term, 'OR');
    	$sql = $this->select();
        $result = $this->db->query($sql);
        return  $this->resultToArray($result);
    }

    /**
     *  Return the first register that represents the title.
     *  Please pass it title
     */
    function getByTitle($title)
    {
        $this->clear();
        $this->addWhere('upper(title) like','%'.strtoupper($title).'%');
        $sql = $this->select();
        $result = $this->db->query($sql);
        $result = $this->resultToArray($result);
        return $result[0];
    }

	/**
	 * Make insert or update in datadb...
	 *
	 */
    public function insert($values)
    {
        str_replace('\n', '', $values['news'] );

      	if ($values['id'] == null)
		{
			unset($values['id']);
            $values['regDate']  = date('Y-m-d');
        	$values['regTime']  = date('H:i');
        	$sql = parent::insert($values);
		}
		else
		{
			$this->clear();
			$this->addWhere('id=', $values['id']);
			$sql = parent::update($values);
		}
        $result = $this->db->query($sql);
        return $result;

    }



    /**
    * List all news registered.
    *
    */
    function listAll()
    {
        $this->clear();
        $sql    = $this->select();
        $result = $this->db->query($sql);
        return $result;
    }


    function listLast($limit=null, $flags=null)
    {
        $this->clear();
        $this->setOrderBy('id desc');
        if ($limit)
        {
            $this->setLimit(0, $limit);
        }
        if ($flags)
        {
            $this->addWhere('flags=', $flags);
        }
        $sql    = $this->select();
        $result = $this->db->query($sql);
        $return = $this->resultToArray($result);

        if ($return)
        {
            foreach ($return as $line => $info)
            {
                //$return[$line]['news'] = str_replace(array("\r", "\n", "\r\n", "\n\r", '\r', '\n', '\r\n', '\n\r'), '', $return[$line]['news'] );
                //$return[$line]['news'] = str_replace('"', '\'', $return[$line]['news'] );
                $return[$line]['news'] = str_replace('monotype=""', '', $return[$line]['news']);
                $return[$line]['news'] = str_replace('corsiva=""', '', $return[$line]['news']);
            }
        }
        return $return;
    }

    /**
    * Remove an User, passing it Id.
    *
    */
    function delete($id)
    {
        $this->clear();
        $this->addWhere("id  ="  , $id);
        return $this->db->query( parent::delete() );
    }

	/**
	 * 	Function to get a News.
     *  Please pass it id.
	 */
    function get($id)
    {
        $this->clear();
        $this->addWhere('id=',$id);
        $sql = $this->select();
        $result = $this->db->query($sql);
        return $result;
    }
}
